package com.aai.servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aai.db.MySQLConnect;

public class EventsResultSetServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5676499465604590262L;

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		MySQLConnect sql_events = MySQLConnect.getInstance();
		Connection connec_events = sql_events.connect();
		if (connec_events != null) {
			String query_events = "SELECT * FROM events ORDER BY date, time DESC";
			Statement st_events;
			int i_events=0;
			ResultSet result_events = null;
			try {
				st_events = connec_events.createStatement();
			    result_events = st_events.executeQuery(query_events);
				result_events.last();
			    i_events = result_events.getRow();
				result_events.first();

			} catch (SQLException e) {
				e.printStackTrace();
			}
	
			if (i_events == 0) {
				RequestDispatcher dispatch = request
						.getRequestDispatcher("/events.jsp?message=noevents");
				dispatch.forward(request, response);
			} else {
				request.setAttribute("eventResultSet",
						(ResultSet) result_events);
				RequestDispatcher dispatch = request
						.getRequestDispatcher("/events.jsp?message=ok");
				dispatch.forward(request, response);
			}
		}
	}
}
